/*
 * OpenTicketView.java
 *
 * Created on August 16, 2006, 3:32 PM
 */

package com.floreantpos.ui.views;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import com.floreantpos.config.UIConfig;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.dao.TicketDAO;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.PosButton;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.views.order.RootView;
import com.floreantpos.util.PanelTester;

/**
 * 
 * @author MShahriar
 */
public class OpenTicketView extends TransparentPanel implements ActionListener {
	public final static String VIEW_NAME = "OPENTICKET_VIEW";

	private CardLayout cardLayout = new CardLayout();

	private Dimension buttonDimension = new Dimension(100, 50);

	/** Creates new form OpenTicketView */
	public OpenTicketView() {
		initComponents();

		String[][] numbers = { { "7", "8", "9" }, 
							   { "4", "5", "6" },
							   { "1", "2", "3" }, 
							   { "0", com.floreantpos.POSConstants.GO } };
		int[][] gridwidths = { { 1, 1, 1 }, 
							   { 1, 1, 1 }, 
							   { 1, 1, 1 }, 
							   { 1, 2 } };

		ticketNumberPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.insets = new Insets(3, 3, 3, 3);
		gbc.fill = GridBagConstraints.BOTH;

		Dimension preferredSize = new Dimension(50, 60);

		for (int i = 0; i < numbers.length; i++) {
			gbc.gridy = i;
			for (int j = 0; j < numbers[i].length; j++) {
				gbc.gridx = j;
				gbc.gridwidth = gridwidths[i][j];
				PosButton posButton = new PosButton();
				posButton.setText(String.valueOf(numbers[i][j]));
				posButton.setActionCommand(String.valueOf(numbers[i][j]));
				posButton.setPreferredSize(preferredSize);
				posButton.addActionListener(this);
				ticketNumberPanel.add(posButton, gbc);
			}
		}

		ticketListPanel.setLayout(cardLayout);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        openTicketsPanel = new com.floreantpos.swing.TransparentPanel();
        buttonPanel = new com.floreantpos.swing.TransparentPanel();
        btnPrev = new com.floreantpos.swing.PosButton();
        btnNext = new com.floreantpos.swing.PosButton();
        btnCancel = new com.floreantpos.swing.PosButton();
        ticketListPanel = new com.floreantpos.swing.TransparentPanel();
        btnPanel = new com.floreantpos.swing.TransparentPanel();
        transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
        ticketNumberOuterPanel = new com.floreantpos.swing.TransparentPanel();
        transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
        btnGo = new com.floreantpos.swing.PosButton();
        tfTicketNumber = new javax.swing.JTextField();
        ticketNumberPanel = new com.floreantpos.swing.TransparentPanel();
        titlePanel = new com.floreantpos.ui.TitlePanel();

        setLayout(new java.awt.BorderLayout(10, 0));

        setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setOpaque(true);
        openTicketsPanel.setLayout(new java.awt.BorderLayout(5, 5));

        openTicketsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Open Tickets", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 5, 0));

        buttonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 3, 1, 3));
        buttonPanel.setPreferredSize(new java.awt.Dimension(100, 50));
        btnPrev.setText("PREV");
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doGoPrev(evt);
            }
        });

        buttonPanel.add(btnPrev);

        btnNext.setText("NEXT");
        btnNext.setPreferredSize(new java.awt.Dimension(100, 50));
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doGoNext(evt);
            }
        });

        buttonPanel.add(btnNext);

        btnCancel.setText("CANCEL");
        btnCancel.setPreferredSize(new java.awt.Dimension(220, 50));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doCancel(evt);
            }
        });

        buttonPanel.add(btnCancel);

        openTicketsPanel.add(buttonPanel, java.awt.BorderLayout.SOUTH);

        org.jdesktop.layout.GroupLayout ticketListPanelLayout = new org.jdesktop.layout.GroupLayout(ticketListPanel);
        ticketListPanel.setLayout(ticketListPanelLayout);
        ticketListPanelLayout.setHorizontalGroup(
            ticketListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 444, Short.MAX_VALUE)
        );
        ticketListPanelLayout.setVerticalGroup(
            ticketListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 239, Short.MAX_VALUE)
        );
        openTicketsPanel.add(ticketListPanel, java.awt.BorderLayout.CENTER);

        add(openTicketsPanel, java.awt.BorderLayout.CENTER);

        btnPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        add(btnPanel, java.awt.BorderLayout.SOUTH);

        transparentPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        ticketNumberOuterPanel.setLayout(new java.awt.BorderLayout(5, 5));

        ticketNumberOuterPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Enter Ticket Number", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        transparentPanel1.setLayout(new java.awt.BorderLayout(5, 5));

        transparentPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 3, 1, 3));
        btnGo.setText("CLEAR");
        btnGo.setPreferredSize(new java.awt.Dimension(60, 50));
        btnGo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doClearTicketNumber(evt);
            }
        });

        transparentPanel1.add(btnGo, java.awt.BorderLayout.EAST);

        tfTicketNumber.setColumns(5);
        tfTicketNumber.setEditable(false);
        tfTicketNumber.setFont(UIConfig.labelLargeFont);
        transparentPanel1.add(tfTicketNumber, java.awt.BorderLayout.CENTER);

        ticketNumberOuterPanel.add(transparentPanel1, java.awt.BorderLayout.NORTH);

        org.jdesktop.layout.GroupLayout ticketNumberPanelLayout = new org.jdesktop.layout.GroupLayout(ticketNumberPanel);
        ticketNumberPanel.setLayout(ticketNumberPanelLayout);
        ticketNumberPanelLayout.setHorizontalGroup(
            ticketNumberPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 224, Short.MAX_VALUE)
        );
        ticketNumberPanelLayout.setVerticalGroup(
            ticketNumberPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 29, Short.MAX_VALUE)
        );
        ticketNumberOuterPanel.add(ticketNumberPanel, java.awt.BorderLayout.CENTER);

        transparentPanel2.add(ticketNumberOuterPanel);

        add(transparentPanel2, java.awt.BorderLayout.WEST);

        add(titlePanel, java.awt.BorderLayout.NORTH);

    }// </editor-fold>//GEN-END:initComponents

    private void doCancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doCancel
    	RootView.getInstance().showView(SwitchboardView.VIEW_NAME);
    }//GEN-LAST:event_doCancel

    private void doGoNext(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doGoNext
    	cardLayout.next(ticketListPanel);
    }//GEN-LAST:event_doGoNext

    private void doGoPrev(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doGoPrev
    	cardLayout.previous(ticketListPanel);
    }//GEN-LAST:event_doGoPrev

	private void doClearTicketNumber(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_doClearTicketNumber
		tfTicketNumber.setText("");
	}// GEN-LAST:event_doClearTicketNumber

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.floreantpos.swing.PosButton btnCancel;
    private com.floreantpos.swing.PosButton btnGo;
    private com.floreantpos.swing.PosButton btnNext;
    private com.floreantpos.swing.TransparentPanel btnPanel;
    private com.floreantpos.swing.PosButton btnPrev;
    private com.floreantpos.swing.TransparentPanel buttonPanel;
    private com.floreantpos.swing.TransparentPanel openTicketsPanel;
    private javax.swing.JTextField tfTicketNumber;
    private com.floreantpos.swing.TransparentPanel ticketListPanel;
    private com.floreantpos.swing.TransparentPanel ticketNumberOuterPanel;
    private com.floreantpos.swing.TransparentPanel ticketNumberPanel;
    private com.floreantpos.ui.TitlePanel titlePanel;
    private com.floreantpos.swing.TransparentPanel transparentPanel1;
    private com.floreantpos.swing.TransparentPanel transparentPanel2;
    // End of variables declaration//GEN-END:variables

	public static void main(String[] args) {
		OpenTicketView openTicketView = new OpenTicketView();
		openTicketView.setVisible(true);
		PanelTester.test(openTicketView);
	}

	public void setVisible(boolean visible) {
		if (visible) {
			updateView();
		}
		else {
			ticketListPanel.removeAll();
		}
		super.setVisible(visible);
	}

	public void updateView() {
		try {
			TicketDAO dao = new TicketDAO();
			List<Ticket> openTickets = dao.findOpenTickets();

			TransparentPanel ticketPanel = null;
			int count = 0;
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.weightx = 1;
			gbc.gridx = 0;
			gbc.gridy = GridBagConstraints.RELATIVE;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.insets = new Insets(3, 3, 3, 3);

			for (Iterator iter = openTickets.iterator(); iter.hasNext(); count++) {
				Ticket ticket = (Ticket) iter.next();
				if (count % 6 == 0) {
					ticketPanel = new TransparentPanel(new GridBagLayout());
					ticketListPanel.add(ticketPanel, String.valueOf(count));
				}
				ticketPanel.add(new TicketButton(ticket), gbc);
			}
			openTickets.clear();
		} catch (Exception e) {
			MessageDialog.showError(e);
		}
	}

	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals(com.floreantpos.POSConstants.GO)) {
			try {
				TicketDAO dao = new TicketDAO();
				int ticketId = 0;
				try {
					ticketId = Integer.parseInt(tfTicketNumber.getText());
				} catch (NullPointerException x) {
				}
				Ticket ticket = dao.get(ticketId);
				if (ticket == null) {
					MessageDialog.showError("Ticket not found");
					return;
				}
				SettleTicketView view = SettleTicketView.getInstance();
				view.setCurrentTicket(ticket);
				RootView.getInstance().showView(SettleTicketView.VIEW_NAME);
			} catch (Exception ex) {
				MessageDialog.showError(ex);
			}
		}

		else {
			tfTicketNumber.setText(tfTicketNumber.getText() + actionCommand);
		}
	}

	class TicketButton extends PosButton implements ActionListener {
		Ticket ticket;

		public TicketButton(Ticket t) {
			this.ticket = t;

			setText(ticket.getTitle());
			setPreferredSize(buttonDimension);
			addActionListener(this);
		}

		public void actionPerformed(ActionEvent e) {
			SettleTicketView view = SettleTicketView.getInstance();
			view.setCurrentTicket(ticket);
			RootView.getInstance().showView(SettleTicketView.VIEW_NAME);
		}

		@Override
		protected void finalize() throws Throwable {
			ticket = null;
			removeActionListener(this);
			super.finalize();
		}
	}
}
